use dta/SMDLevel.dta, clear

gen SECOND=0
replace SECOND=1 if votes2_TOTAL!=0  

**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************

*********************** CREATE BLOCS **********************************************

foreach round in 1 2 {
gen vs`round'_LAB=vs`round'_s+vs`round'_nsa+vs`round'_nkp 
gen vs`round'_LIB=vs`round'_a+vs`round'_v_a+vs`round'_v 
gen vs`round'_CON=vs`round'_fv+vs`round'_h_fv+vs`round'_h 
gen vs`round'_OTH=1-vs`round'_LAB-vs`round'_LIB-vs`round'_CON
}

foreach bloc in LAB LIB CON OTH {
gen vsF_`bloc'=vs1_`bloc'
replace vsF_`bloc'=vs2_`bloc' if SECOND==1
}

*** generating leads and lags
xtset SMD year

foreach round in 1 2 F {
	foreach bloc in LAB LIB CON OTH {
	gen next_vs`round'_`bloc'=f3.vs`round'_`bloc'
	gen prev_vs`round'_`bloc'=l3.vs`round'_`bloc'
	gen diff`round'_`bloc'=vs`round'_`bloc'-l3.vs`round'_`bloc'
	}
}

keep year SMD PR *vs*LAB* *vs*LIB* *vs*CON* *vs*OTH* diff*
order year SMD PR vs1* vs2* vsF* next_vs1* next_vs2* next_vsF* prev_vs1* prev_vs2* prev_vsF*

**********************************************************************************
**********************************************************************************
**********************************************************************************

gen AdvantageLibCon=(vs1_LIB-vs1_CON)/(vs1_CON+vs1_LIB)
gen AdvantageLibLab=(vs1_LIB-vs1_LAB)/(vs1_LIB+vs1_LAB)
gen AdvantageLabCon=(vs1_LAB-vs1_CON)/(vs1_LAB+vs1_CON)

foreach var in AdvantageLibCon AdvantageLibLab AdvantageLabCon {
gen lag_`var'=l3.`var'
}

sort SMD
merge SMD using dta/StableSample

sum lag_Advantage* if year==1921 & sample==1
corr lag_Advantage* if year==1921 & sample==1

**********************************************************************************
**********************************************************************************
**********************************************************************************

gen SMD_label="."
****replace SMD_label="District A" if SMD==104 /* alternative example district */
replace SMD_label="District A" if SMD==1800
replace SMD_label="District B" if SMD==1802


**********************************************************************************
*********************    FIGURE: LibCon  *****************************************
**********************************************************************************

foreach year in 1912 {
	foreach advantage in LibCon {

			foreach bloc in LAB LIB CON OTH {
			twoway (lfitci diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, lwidth(thick) estopts(robust)) ///
			(scatter diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, msymbol(Oh) msize(large)), ///
			ylabel(-0.4(0.2)0.4,angle(0)) yscale(range(-0.5 0.5)) xscale(range(0 1) titlegap(*5) off)  ///
			ytitle("ΔVS `bloc'")	 xtitle("") ///
			legend(off) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) 
			graph save figures/gph/diff1_`bloc'.gph, replace
			}
			
	cd figures/gph
	graph combine diff1_LIB.gph diff1_LAB.gph diff1_CON.gph diff1_OTH.gph, xcommon ycommon plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) title(A: Non-reform `year')
	graph export ../Figure2/Adv`advantage'`year'.pdf, replace
	cd ../../
	}
}


foreach year in 1915 {
	foreach advantage in LibCon {

			foreach bloc in LAB LIB CON OTH {
			twoway (lfitci diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, lwidth(thick) estopts(robust)) ///
			(scatter diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, msymbol(Oh) msize(large)), ///
			ylabel(-0.4(0.2)0.4,angle(0)) yscale(range(-0.5 0.5)) xscale(range(0 1) titlegap(*5) off)  ///
			ytitle("ΔVS `bloc'")	 xtitle("") ///
			legend(off) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) 
			graph save figures/gph/diff1_`bloc'.gph, replace
			}
			
	cd figures/gph
	graph combine diff1_LIB.gph diff1_LAB.gph diff1_CON.gph diff1_OTH.gph, xcommon ycommon plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) title(B: Non-reform `year')
	graph export ../Figure2/Adv`advantage'`year'.pdf, replace
	cd ../../
	}
}

foreach year in 1918 {
	foreach advantage in LibCon {

			foreach bloc in LAB LIB CON OTH {
			twoway (lfitci diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, lwidth(thick) estopts(robust)) ///
			(scatter diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, msymbol(Oh) msize(large)), ///
			ylabel(-0.4(0.2)0.4,angle(0)) yscale(range(-0.5 0.5)) xscale(range(0 1) titlegap(*5) off)  ///
			ytitle("ΔVS `bloc'")	 xtitle("") ///
			legend(off) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) 
			graph save figures/gph/diff1_`bloc'.gph, replace
			}
			
	cd figures/gph
	graph combine diff1_LIB.gph diff1_LAB.gph diff1_CON.gph diff1_OTH.gph, xcommon ycommon plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) title(C: Non-reform `year')
	graph export ../Figure2/Adv`advantage'`year'.pdf, replace
	cd ../../
	}
}


foreach year in 1921 {
	foreach advantage in LibCon {

			foreach bloc in LAB LIB CON OTH {
			twoway (lfitci diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, lwidth(thick) estopts(robust)) ///
			(scatter diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, msymbol(Oh) msize(large)), ///
			ylabel(-0.4(0.2)0.4,angle(0)) yscale(range(-0.5 0.5)) xscale(range(0 1) titlegap(*5) off)  ///
			ytitle("ΔVS `bloc'")	 xtitle("") ///
			legend(off) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) 
			graph save figures/gph/diff1_`bloc'.gph, replace
			}

			
	cd figures/gph
	graph combine diff1_LIB.gph diff1_LAB.gph diff1_CON.gph diff1_OTH.gph, xcommon ycommon plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) title(D: Reform `year')
	graph export ../Figure2/Adv`advantage'`year'.pdf, replace
	cd ../../
	}
}


foreach year in 1924 {
	foreach advantage in LibCon {

			foreach bloc in LAB LIB {
			twoway (lfitci diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, lwidth(thick) estopts(robust)) ///
			(scatter diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, msymbol(Oh) msize(large)), ///
			ylabel(-0.4(0.2)0.4,angle(0)) yscale(range(-0.5 0.5)) xscale(range(0 1) titlegap(*5) off)  ///
			ytitle("ΔVS `bloc'")	 xtitle("") ///
			legend(off) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) 
			graph save figures/gph/diff1_`bloc'.gph, replace
			}

	
			foreach bloc in CON OTH {
			twoway (lfitci diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, lwidth(thick) estopts(robust)) ///
			(scatter diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, msymbol(Oh) msize(large)), ///
			ylabel(-0.4(0.2)0.4,angle(0)) yscale(range(-0.5 0.5)) xscale(range(0 1) titlegap(*5))  ///
			ytitle("ΔVS `bloc'")	 xtitle("LIB advantage over CON") ///
			legend(off) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) 
			graph save figures/gph/diff1_`bloc'.gph, replace
			}
			
	cd figures/gph
	graph combine diff1_LIB.gph diff1_LAB.gph diff1_CON.gph diff1_OTH.gph, xcommon ycommon plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) title(E: Non-reform `year')
	graph export ../Figure2/Adv`advantage'`year'.pdf, replace
	cd ../../
	}
}


foreach year in 1927 {
	foreach advantage in LibCon {

			foreach bloc in LAB LIB {
			twoway (lfitci diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, lwidth(thick) estopts(robust)) ///
			(scatter diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, msymbol(Oh) msize(large)), ///
			ylabel(-0.4(0.2)0.4,angle(0)) yscale(range(-0.5 0.5)) xscale(range(0 1) titlegap(*5) off)  ///
			ytitle("ΔVS `bloc'")	 xtitle("") ///
			legend(off) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) 
			graph save figures/gph/diff1_`bloc'.gph, replace
			}

	
			foreach bloc in CON OTH {
			twoway (lfitci diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, lwidth(thick) estopts(robust)) ///
			(scatter diff1_`bloc' lag_Advantage`advantage' if year==`year' & sample==1, msymbol(Oh) msize(large)), ///
			ylabel(-0.4(0.2)0.4,angle(0)) yscale(range(-0.5 0.5)) xscale(range(0 1) titlegap(*5))  ///
			ytitle("ΔVS `bloc'")	 xtitle("LIB advantage over CON") ///
			legend(off) plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) 
			graph save figures/gph/diff1_`bloc'.gph, replace
			}
			
	cd figures/gph
	graph combine diff1_LIB.gph diff1_LAB.gph diff1_CON.gph diff1_OTH.gph, xcommon ycommon plotregion(lcolor(white) ilcolor(white)) graphregion(fcolor(white)) scheme(s1mono) title(F: Non-reform `year')
	graph export ../Figure2/Adv`advantage'`year'.pdf, replace
	cd ../../
	}
}

**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
**********************************************************************************
